/******************************************************************************

STATA do-file for Section D.6 of "Voters rally around the incumbent in the aftermath of terrorist attacks: Evidence from multiple unexpected events during surveys"
by Albert Falcó-Gimeno, Jordi Muñoz, & Roberto Pannico

Political Science Research and Methods

Last update: 10/02/2025


*******************************************************************************/

log using "Placebo Preplication (Section D.6).log", replace


/*******************************************************************************
                       HARMONIZATION OF THE VARIABLES
*******************************************************************************/



*************************** OCTOBER 1998 ***************************************

import spss using "CIS Studies\2307.sav", clear


** date
gen str_date= substr(FINAL,18,6) 
tab str_date
replace str_date="" if str_date=="  1098" // for this respondent, the day of the interview is not available

gen date= date(str_date, "DMY", 2000) 
format date %tdDDmonCCYY 
replace date =. if date<td(26oct1998) | date>td(31oct1998) 


** incumbent
gen votinc=1 if P33==2
replace votinc=0 if P33!=2 & P33!=.
label variable votinc "Vote for the incumbent"


** gender
gen female=P38-1


** age
gen age=P39


** education
recode P40A (1=1) (2=2) (3=3) (4=4) (6=5) (5=6) (7=7) (8/11=8) (13=9) (12=10) (14 99=.), gen(edu)


** employment status
recode P42 (1=1 "employed") (4 5 =2 "unemployed") (6=3 "student") (2 3 =4 "retired") (7=5 "housework") (8 9 =.), gen(emplo)  


** size of municipality
clonevar munsz=TAMUNI


** ccaa
rename CCAA ccaa


** survey id
gen studyid="1098"


** random treatment variable 
preserve

bysort date: gen date_unique= _n ==1 
replace date_unique=. if date==.
keep if date_unique==1
set seed 1
sample 1, count 
scalar random_attdate = date 

restore

display %tdDDmonCCYY random_attdate
gen randdate= random_attdate // creating a variable with the random date
format randdate %tdDDmonCCYY
scalar drop random_attdate

gen randtreat = 1 if date>randdate & date!=. // creating a variable for the random treatment
replace randtreat = 0 if date<randdate
replace randtreat = . if date==randdate
label variable randtreat "random treatment"
label define treat 0 "pre" 1 "post"
label values randtreat treat



save "harmonized dataset\1998_10", replace  




*************************** JANUARY 1999 ***************************************

import spss using "CIS Studies\2316.sav", clear


** date
gen str_date= substr(FINAL,18,6) 

gen date= date(str_date, "DMY", 2000) 
format date %tdDDmonCCYY 
replace date =. if date<td(27jan1999) | date>td(31jan1999) 

** incumbent
gen votinc=1 if P33==2
replace votinc=0 if P33!=2 & P33!=.
label variable votinc "Vote for the incumbent"


** gender
gen female=P37-1


** age
gen age=P38
replace age=. if age==99


** education
recode P39A (1=1) (2=2) (3=3) (4=4) (6=5) (5=6) (7=7) (8/11=8) (13=9) (12=10) (14 99=.), gen(edu)


** employment status
recode P41 (1=1 "employed") (4 5 =2 "unemployed") (6=3 "student") (2 3 =4 "retired") (7=5 "housework") (8 9 =.), gen(emplo)  


** size of municipality
clonevar munsz=TAMUNI


** ccaa
rename CCAA ccaa


** survey id
gen studyid="0199"


** random treatment variable 
preserve

bysort date: gen date_unique= _n ==1 
replace date_unique=. if date==.
keep if date_unique==1
set seed 12
sample 1, count 
scalar random_attdate = date 

restore

display %tdDDmonCCYY random_attdate
gen randdate= random_attdate // creating a variable with the random date
format randdate %tdDDmonCCYY
scalar drop random_attdate

gen randtreat = 1 if date>randdate & date!=. // creating a variable for the random treatment
replace randtreat = 0 if date<randdate
replace randtreat = . if date==randdate
label variable randtreat "random treatment"
label define treat 0 "pre" 1 "post"
label values randtreat treat


save "harmonized dataset\1999_01", replace  




***************************** APRIL 1999 ***************************************

import spss using "CIS Studies\2324.sav", clear


** date
gen str_date= substr(FINAL,18,6) 
tab str_date
replace str_date="" if str_date=="  0499" // for this respondent, the day of the interview is not available

gen date= date(str_date, "DMY", 2000) 
format date %tdDDmonCCYY 
replace date =. if date<td(24apr1999) | date>td(28apr1999) 

** incumbent
gen votinc=1 if P34==2
replace votinc=0 if P34!=2 & P34!=.
label variable votinc "Vote for the incumbent"


** gender
gen female=P38-1


** age
gen age=P39


** education
recode P40A (1=1) (2=2) (3=3) (4=4) (6=5) (5=6) (7=7) (8/11=8) (13=9) (12=10) (14 99=.), gen(edu)


** employment status
recode P42 (1=1 "employed") (4 5 =2 "unemployed") (6=3 "student") (2 3 =4 "retired") (7=5 "housework") (8 9 =.), gen(emplo)  


** size of municipality
clonevar munsz=TAMUNI


** ccaa
rename CCAA ccaa


** survey id
gen studyid="0499"


** random treatment variable 
preserve

bysort date: gen date_unique= _n ==1 
replace date_unique=. if date==.
keep if date_unique==1
set seed 222
sample 1, count 
scalar random_attdate = date 

restore

display %tdDDmonCCYY random_attdate
gen randdate= random_attdate // creating a variable with the random date
format randdate %tdDDmonCCYY
scalar drop random_attdate

gen randtreat = 1 if date>randdate & date!=. // creating a variable for the random treatment
replace randtreat = 0 if date<randdate
replace randtreat = . if date==randdate
label variable randtreat "random treatment"
label define treat 0 "pre" 1 "post"
label values randtreat treat


save "harmonized dataset\1999_04", replace  




***************************** JULY 1999 ****************************************

import spss using "CIS Studies\2367.sav", clear


** date
gen str_date= substr(FINAL,18,6) 
tab str_date
replace str_date="" if str_date=="999999" // for this respondent, the date of the interview is not available

gen date= date(str_date, "DMY", 2000) 
format date %tdDDmonCCYY 
replace date =. if date<td(09jul1999) | date>td(15jul1999) 


** incumbent
gen votinc=1 if P25==2
replace votinc=0 if P25!=2 & P25!=.
label variable votinc "Vote for the incumbent"


** gender
gen female=P30-1


** age
gen age=P31


** education
recode P32A (1=1) (2=2) (3=3) (4=4) (6=5) (5=6) (7=7) (8/11=8) (13=9) (12=10) (14 99=.), gen(edu)


** employment status
recode P34 (1=1 "employed") (4 5 =2 "unemployed") (6=3 "student") (2 3 =4 "retired") (7=5 "housework") (8 9 =.), gen(emplo)  


** size of municipality
clonevar munsz=TAMUNI


** ccaa
rename CCAA ccaa


** survey id
gen studyid="0799"


** random treatment variable 
preserve

bysort date: gen date_unique= _n ==1 
replace date_unique=. if date==.
keep if date_unique==1
set seed 2112
sample 1, count 
scalar random_attdate = date 

restore

display %tdDDmonCCYY random_attdate
gen randdate= random_attdate // creating a variable with the random date
format randdate %tdDDmonCCYY
scalar drop random_attdate

gen randtreat = 1 if date>randdate & date!=. // creating a variable for the random treatment
replace randtreat = 0 if date<randdate
replace randtreat = . if date==randdate
label variable randtreat "random treatment"
label define treat 0 "pre" 1 "post"
label values randtreat treat


save "harmonized dataset\1999_07", replace 




***************************** OCTOBER 1999 *************************************

import spss using "CIS Studies\2372.sav", clear


** date
gen str_date= substr(FINAL,18,6)
tab str_date
replace str_date="" if str_date=="    99" // for this respondent, the day and the month of the interview are not available

gen date= date(str_date, "DMY", 2000) 
format date %tdDDmonCCYY 
replace date =. if date<td(22oct1999) | date>td(26oct1999) 


** incumbent
gen votinc=1 if P26==2
replace votinc=0 if P26!=2 & P26!=.
label variable votinc "Vote for the incumbent"


** gender
gen female=P31-1


** age
gen age=P32
replace age=. if age==99


** education
recode P33A (1=1) (2=2) (3=3) (4=4) (6=5) (5=6) (7=7) (8/11=8) (13=9) (12=10) (14 99=.), gen(edu)


** employment status
recode P35 (1=1 "employed") (4 5 =2 "unemployed") (6=3 "student") (2 3 =4 "retired") (7=5 "housework") (8 9 =.), gen(emplo)   


** size of municipality
clonevar munsz=TAMUNI


** ccaa
rename CCAA ccaa


** survey id
gen studyid="1099"


** random treatment variable  
preserve

bysort date: gen date_unique= _n ==1 
replace date_unique=. if date==.
keep if date_unique==1
set seed 2222
sample 1, count 
scalar random_attdate = date 

restore

display %tdDDmonCCYY random_attdate
gen randdate= random_attdate // creating a variable with the random date
format randdate %tdDDmonCCYY
scalar drop random_attdate

gen randtreat = 1 if date>randdate & date!=. // creating a variable for the random treatment
replace randtreat = 0 if date<randdate
replace randtreat = . if date==randdate
label variable randtreat "random treatment"
label define treat 0 "pre" 1 "post"
label values randtreat treat


save "harmonized dataset\1999_10", replace 






/*******************************************************************************
                                    MERGE
*******************************************************************************/

use "harmonized dataset\1998_10", clear 

append using "harmonized dataset\1999_01" "harmonized dataset\1999_04" "harmonized dataset\1999_07" "harmonized dataset\1999_10" 

keep date votinc female age edu emplo munsz ccaa studyid randtreat randdate
encode studyid, gen(study) // study id
drop studyid
rename study studyid
gen respid = _n // respondent id



** random assignment of the region of the attack: we assign to each of the 5 surveys one of the 5 most represented regions in the attacks used in the main study (i.e. Andalucía, Catalunya, Madrid, Navarra, País Vasco - see Table A2)
sort studyid respid
by studyid: gen first = _n == 1 

preserve
keep if first==1
set seed 666
gen random = runiform() 
sort random 
gen randreg = .
replace randreg = 1 in 1 
replace randreg = 9 in 2
replace randreg = 13 in 3
replace randreg = 15 in 4
replace randreg = 16 in 5
keep respid studyid randreg
save "harmonized dataset\first0", replace
restore

merge 1:1 studyid respid using "harmonized dataset\first0" 
label values randreg labels0
drop first _merge
rm "harmonized dataset\first0.dta"

bysort studyid: replace randreg = randreg[1] 


** creating the dichotomous variable for the region of the attack   
gen regattack = 1 if ccaa==randreg
replace regattack = 0 if ccaa!=randreg
label variable regattack "attacked region"



save "harmonized dataset\allbar", replace // 5-study dataset






/*******************************************************************************
                            CREATION OF X20 DATASET
*******************************************************************************/

use "harmonized dataset\allbar", clear 


forval i = 1/19 {
    append using "harmonized dataset\allbar"
} 


** creating an identifier for each of the 100 surveys in the dataset
local n_obs_per_repetition = _N / 20 
display `n_obs_per_repetition'

gen repetition = floor((_n - 1) / `n_obs_per_repetition') + 1  

sort studyid repetition
egen surid= group(studyid repetition) // survey id



** creating, for each of the surveys, a random date variable. 
bysort surid: egen min_date = min(date) 
bysort surid: egen max_date = max(date) 

set seed 777
gen random_number = runiform() 
bysort surid: replace random_number = random_number[1] 
bysort surid: egen randdate100 = min(min_date + random_number * (max_date - min_date)) 
replace randdate100 = floor(randdate100) 
format randdate100 %tdDDmonCCYY
drop random_number min_date max_date



** creating, for each of the surveys, random treatment variables based on the random date variable 
gen randtreat100 = 1 if date>randdate100 & date!=. 
replace randtreat100 = 0 if date<randdate100 
replace randtreat100 = . if date==randdate100
label variable randtreat100 "random treatment for the 100 surveys"
label values randtreat100 treat



** randomly assigning the region of the attack, mirroring the distribution of regions in the attacks used in the main analysis (see Table A2)
sort surid respid
by surid: gen first = _n == 1 

preserve
keep if first==1
set seed 888
gen random = runiform() 
sort random 
gen randreg100 = .
replace randreg100 = 1 in 1/6 
replace randreg100 = 2 in 7/8
replace randreg100 = 16 in 9/56
replace randreg100 = 6 in 57
replace randreg100 = 8 in 58/59
replace randreg100 = 9 in 60/65
replace randreg100 = 12 in 66/68
replace randreg100 = 17 in 69
replace randreg100 = 13 in 70/94
replace randreg100 = 15 in 95/99
replace randreg100 = 10 in 100
keep respid surid randreg100
save "harmonized dataset\first", replace
restore

merge 1:1 surid respid using "harmonized dataset\first" 
label values randreg100 labels0
drop first _merge
rm "harmonized dataset\first.dta"

bysort surid: replace randreg100 = randreg100[1] 



** creating the dichotomous variable for the region of the attack   
gen regattack100 = 1 if ccaa==randreg100
replace regattack100 = 0 if ccaa!=randreg100
label variable regattack100 "attacked region"




** "eliminating" UESDs with only "pre" or only "post" observations 
bysort surid: egen n_pre = total(randtreat100 == 0) 
bysort surid: egen n_post = total(randtreat100 == 1) 
gen tag = 0
replace tag = 1 if n_pre == 0 | n_post == 0 
replace randtreat100=. if tag==1 
drop n_pre n_post tag



save "harmonized dataset\allbar20", replace




/*******************************************************************************
                         MODELS FOR THE MERGED (5-SURVEY) DATASET
*******************************************************************************/

use "harmonized dataset\allbar", clear

** needed for the balance tests
tabulate emplo, gen(em)
tabulate munsz, gen(mu)


** balance tests
reg female i.randtreat##i.regattack i.studyid 

reg age i.randtreat##i.regattack i.studyid 

reg edu i.randtreat##i.regattack i.studyid 

reg em1 i.randtreat##i.regattack i.studyid 
reg em2 i.randtreat##i.regattack i.studyid 
reg em3 i.randtreat##i.regattack i.studyid 
reg em4 i.randtreat##i.regattack i.studyid 
reg em5 i.randtreat##i.regattack i.studyid 

reg mu1 i.randtreat##i.regattack i.studyid 
reg mu2 i.randtreat##i.regattack i.studyid 
reg mu3 i.randtreat##i.regattack i.studyid  
reg mu4 i.randtreat##i.regattack i.studyid 
reg mu5 i.randtreat##i.regattack i.studyid 
reg mu6 i.randtreat##i.regattack i.studyid 
reg mu7 i.randtreat##i.regattack i.studyid  




** main effect & interaction models (TABLE D6)
eststo clear 

eststo bas: reg votinc i.randtreat i.ccaa i.studyid
estadd local controls "No"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo bascon: reg votinc i.randtreat female c.age##c.age edu i.emplo i.munsz i.ccaa i.studyid 
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo basint: reg votinc i.randtreat##i.regattack i.ccaa i.studyid 
estadd local controls "No"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo basinccon: reg votinc i.randtreat##i.regattack female c.age##c.age edu i.emplo i.munsz i.ccaa i.studyid 
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo basincconimb: reg votinc i.randtreat##i.regattack female c.age##c.age edu i.emplo##i.regattack i.munsz##i.regattack i.ccaa i.studyid 
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "Yes"
estadd local n = e(N)



** TABLE D6
esttab bas bascon basint basinccon basincconimb ///
, replace noomitted label nonotes noobs nodepvar nomtitles ///
keep(1.randtreat 1.regattack 1.randtreat#1.regattack) ///
 coeflabels(1.randtreat "Post" 1.regattack "Target region" 1.randtreat#1.regattack "Post X Target") ///
 b(%9.2f) se(%9.2f) ///
 scalars("controls Controls" "attack_fe False Attack FE" "region_fe Region FE" "imb Imbalance inter." "n N. of observations") ///
 starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 addnotes("Standard errors in parentheses" "+ p<.10, * p<.05, ** p<.01, *** p<.001.")

eststo clear 



/*******************************************************************************
                         MODELS FOR X20 DATASET
*******************************************************************************/


use "harmonized dataset\allbar20", clear


** needed for the balance tests
tabulate emplo, gen(em)
tabulate munsz, gen(mu)


** balance tests
reg female i.randtreat100##i.regattack100 i.studyid i.randdate100 

reg age i.randtreat100##i.regattack100 i.studyid  i.randdate100 

reg edu i.randtreat100##i.regattack100 i.studyid  i.randdate100  

reg em1 i.randtreat100##i.regattack100 i.studyid  i.randdate100  
reg em2 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg em3 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg em4 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg em5 i.randtreat100##i.regattack100 i.studyid i.randdate100  

reg mu1 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu2 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu3 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu4 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu5 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu6 i.randtreat100##i.regattack100 i.studyid i.randdate100  
reg mu7 i.randtreat100##i.regattack100 i.studyid i.randdate100  



** main effect & interaction models (TABLE D7)
eststo clear 

eststo bas100: reg votinc i.randtreat100 i.ccaa i.randdate100, vce(cluster respid) 
estadd local controls "No"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo bascon100: reg votinc i.randtreat100 female c.age##c.age edu i.emplo i.munsz i.ccaa i.randdate100, vce(cluster respid) 
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)

eststo basint100: reg votinc i.randtreat100##i.regattack100 i.ccaa i.randdate100, vce(cluster respid) 
estadd local controls "No"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)                

eststo basinccon100: reg votinc i.randtreat100##i.regattack100 female c.age##c.age edu i.emplo i.munsz i.ccaa i.randdate100, vce(cluster respid) 
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "No"
estadd local n = e(N)
                   
eststo basincconimb100: reg votinc i.randtreat100##i.regattack100 female c.age##c.age edu i.emplo##i.regattack100 i.munsz##i.regattack100 i.ccaa i.randdate100, vce(cluster respid)  
estadd local controls "Yes"
estadd local attack_fe "Yes"
estadd local region_fe "Yes"
estadd local imb "Yes"
estadd local n = e(N)


** TABLE D7
esttab bas100 bascon100 basint100 basinccon100 basincconimb100 ///
, replace noomitted label nonotes noobs nodepvar nomtitles ///
keep(1.randtreat100 1.regattack100 1.randtreat100#1.regattack100) ///
 coeflabels(1.randtreat100 "Post" 1.regattack100 "Target region" 1.randtreat100#1.regattack100 "Post X Target") ///
 b(%9.2f) se(%9.2f) ///
 scalars("controls Controls" "attack_fe False Attack FE" "region_fe Region FE" "imb Imbalance inter." "n N. of observations") ///
 interaction(" $\times$ ") ///
 starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 addnotes("Standard errors in parentheses" "+ p<.10, * p<.05, ** p<.01, *** p<.001.")

eststo clear 

log close